package com.bucg.cjcloud.gb28181.common.service;

import com.bucg.cjcloud.gb28181.common.entity.ItemEntity;
import com.bucg.cjcloud.gb28181.common.entity.PlatformEntity;
import com.bucg.cjcloud.gb28181.common.repository.ItemRepository;
import com.bucg.cjcloud.gb28181.common.repository.PlatformRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceMangeServiceImpl implements DeviceMangeService {
    @Autowired
    private ItemRepository itemRepository;
    @Autowired
    private PlatformRepository platformRepository;

    public String getRealAddr(String deviceId) {
        ItemEntity itemEntity = getItem(deviceId);
        Long platFormId = null;
        String sipAddr = null;
        if (itemEntity != null) {
            platFormId = itemEntity.getPlatFormId();
            PlatformEntity platform = platformRepository.findById(platFormId).get();
            sipAddr = platform.getRealAddr().split(":")[0];
        } else {
            PlatformEntity platform = getPlatform(deviceId);
            // List<ItemEntity> itemByPlatFormId = getItemByPlatFormId(platform.getId());
            sipAddr = platform.getRealAddr().split(":")[0];
        }

        return sipAddr;
    }

    public PlatformEntity getPlatform(String deviceId) {
        PlatformEntity platformEntity = new PlatformEntity();
        platformEntity.setDeviceId(deviceId);
        Example<PlatformEntity> deviceID = Example.of(platformEntity, ExampleMatcher.matching().withMatcher("deviceId", ExampleMatcher.GenericPropertyMatchers.exact()));
        List<PlatformEntity> all = platformRepository.findAll(deviceID);
        if (all.size() > 0) return all.get(0);

        return null;
    }

    @Override
    public List<PlatformEntity> getPlatForms(Long platformId, String pDeviceId) {
        PlatformEntity platformEntity = new PlatformEntity();
        platformEntity.setDeviceId(pDeviceId);
        platformEntity.setId(platformId);
        Example<PlatformEntity> deviceID = Example.of(platformEntity, ExampleMatcher.matching());
        return platformRepository.findAll(deviceID);
    }

    @Override
    public List<ItemEntity> getItems(Long platformId, String pDeviceId, String deviceId) {
        if (pDeviceId != null) {
            PlatformEntity byDeviceId = platformRepository.findByDeviceId(pDeviceId);
            platformId = byDeviceId.getId();
        }
        ItemEntity itemEntity = new ItemEntity();
        itemEntity.setDeviceId(deviceId);
        itemEntity.setPlatFormId(platformId);

        Example<ItemEntity> deviceID = Example.of(itemEntity, ExampleMatcher
                .matching());
        return itemRepository.findAll(deviceID);
    }

    public ItemEntity getItem(String deviceId) {
        ItemEntity itemEntity = new ItemEntity();
        itemEntity.setDeviceId(deviceId);
        Example<ItemEntity> deviceID = Example.of(itemEntity, ExampleMatcher.matching().withMatcher("deviceId", ExampleMatcher.GenericPropertyMatchers.exact()));
        List<ItemEntity> all = itemRepository.findAll(deviceID);
        if (all.size() > 0) return all.get(0);
        return null;
    }

    @Override
    public ItemEntity getItem(Integer ssrcId) {

        ItemEntity itemEntity = new ItemEntity();
        itemEntity.setSsrcId(ssrcId);
        List<ItemEntity> list0 = itemRepository.findAll(Example.of(itemEntity,
                ExampleMatcher.matching().withMatcher("ssrcId", ExampleMatcher.GenericPropertyMatcher::exact)));
        if (list0.size() > 1)
            return list0.get(0);
        // throw new RuntimeException(ssrcId +" not found item");
        return null;
    }

    @Override
    public List<PlatformEntity> getPlatforms() {

        List<PlatformEntity> all = platformRepository.findAll();
        return all;
    }

    @Override
    public List<ItemEntity> getItemByPlatFormId(Long id) {
        ItemEntity itemEntity = new ItemEntity();
        itemEntity.setPlatFormId(id);
        Example<ItemEntity> deviceID = Example.of(itemEntity, ExampleMatcher.matching().withMatcher("platFormId", ExampleMatcher.GenericPropertyMatchers.exact()));
        return itemRepository.findAll(deviceID);
    }


}
